﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using jsm.SqlHelper;

namespace dbcaac
{
    public sealed class EntityJiyuanNRSH : EntityBase
    {
        public string jyrzLogId { get; private set; }
        public string jyId { get; private set; }
        public string jyProjectId { get; private set; }
        public bool option { get; private set; }


        public override string getIdField() { return "jyId"; }
        public override string getPidField() { return "jyParentId"; }

        public override void loadPageData(HttpContext context)
        {
            jyId = context.Request["id"];
            jyrzLogId = context.Request["logid"];
            if (null != context.Request["option"])
            {
                option = bool.Parse(context.Request["option"]);
            }
        }

        protected override int doInsertOperation()
        {
            using (SqlServerHelper helper = new SqlServerHelper())
            {
                helper.Open();
                SqlTransaction transaction = helper.Connection.BeginTransaction();
                helper.Command.Transaction = transaction;
                try
                {
                    int retVal = 0;
                    if (option) // 审核通过
                    {
                        // 1、插入数据（修改标志位，使数据有效）
                        helper.Command.CommandText = @"UPDATE [jiyuanNR] SET jyNewFlag = 0 WHERE jyId = @jyId ";
                        helper.AddParameter("@jyId", SqlDbType.Int, int.Parse(jyId));
                        retVal = helper.ExecuteNoneQuery();

                        // 2、记录日志
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, jyrzApprovalDate = @jyrzApprovalDate,
                                                   jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "APPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        helper.ExecuteNoneQuery();
                    }
                    else // 审核不通过
                    {
                        // 1、标记删除
                        helper.Command.CommandText = @"UPDATE [jiyuanNR] SET jyDeleteFlag = 1 WHERE jyId = @jyId ";
                        helper.AddParameter("@jyId", SqlDbType.Int, int.Parse(jyId));
                        retVal = helper.ExecuteNoneQuery();

                        // 2、记录日志
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, jyrzApprovalDate = @jyrzApprovalDate,
                                                   jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "DISAPPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        helper.ExecuteNoneQuery();
                    }

                    transaction.Commit();
                    return retVal;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        protected override int doDeleteOperation()
        {
            using (SqlServerHelper helper = new SqlServerHelper())
            {
                helper.Open();
                SqlTransaction transaction = helper.Connection.BeginTransaction();
                helper.Command.Transaction = transaction;
                try
                {
                    int retVal = 0;
                    if (option) // 审核通过
                    {
                        // 1、删除数据（修改标志位，使数据无效）           
                        List<SqlParameter> sqlParams = new List<SqlParameter>();
                        sqlParams.Add(new SqlParameter("@nodeId", int.Parse(jyId)));
                        retVal = SqlServerHelper.ExecuteNonQueryProc("dbcaac_DeleteNode", sqlParams.ToArray());

                        // 2、记录日志
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, 
                                                        jyrzApprovalDate = @jyrzApprovalDate,
                                                        jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "APPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        helper.ExecuteNoneQuery();
                    }
                    else // 审核不通过
                    {
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, 
                                                        jyrzApprovalDate = @jyrzApprovalDate,
                                                        jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "DISAPPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        helper.ExecuteNoneQuery();
                    }

                    transaction.Commit();
                    return retVal;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        protected override int doUpdateOperation()
        {
            using (SqlServerHelper helper = new SqlServerHelper())
            {
                helper.Open();
                SqlTransaction transaction = helper.Connection.BeginTransaction();
                helper.Command.Transaction = transaction;
                try
                {
                    int retVal = 0;
                    if (option) // 审核通过
                    {
                        // 1、从日志中取得修改后的数据
                        string sqlText = string.Format("SELECT * FROM [jiyuanNRLog] WHERE jyrzLogId = '{0}'", jyrzLogId);
                        SqlDataReader newContent = SqlServerHelper.ExecuteReader(sqlText);
                        newContent.Read();

                        // 2、修改原有数据
                        helper.Command.CommandText = @"UPDATE [jiyuanNR] SET jyName = @jyName, jyContent = @jyContent, jyStandard = @jyStandard,
                                jyBasis = @jyBasis, jyPenalizeRef = @jyPenalizeRef, jyIsMust = @jyIsMust, jyIsTemp = @jyIsTemp WHERE jyId = @jyId ";
                        helper.AddParameter("@jyId", SqlDbType.Int, int.Parse(jyId));
                        helper.AddParameter("@jyName", SqlDbType.VarChar, newContent.GetSqlValue(newContent.GetOrdinal("jyrzName")));
                        helper.AddParameter("@jyContent", SqlDbType.NText, newContent.GetSqlValue(newContent.GetOrdinal("jyrzContent")));
                        helper.AddParameter("@jyStandard", SqlDbType.NText, newContent.GetSqlValue(newContent.GetOrdinal("jyrzStandard")));
                        helper.AddParameter("@jyBasis", SqlDbType.NText, newContent.GetSqlValue(newContent.GetOrdinal("jyrzBasis")));
                        helper.AddParameter("@jyPenalizeRef", SqlDbType.NText, newContent.GetSqlValue(newContent.GetOrdinal("jyrzPenalizeRef")));
                        helper.AddParameter("@jyIsMust", SqlDbType.Bit, newContent.GetSqlValue(newContent.GetOrdinal("jyrzIsMust")));
                        helper.AddParameter("@jyIsTemp", SqlDbType.Bit, newContent.GetSqlValue(newContent.GetOrdinal("jyrzIsTemp")));

                        retVal = helper.ExecuteNoneQuery();

                        // 3、记录日志
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, 
                                                        jyrzApprovalDate = @jyrzApprovalDate,
                                                        jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "APPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        helper.ExecuteNoneQuery();
                    }
                    else // 审核不通过
                    {
                        helper.Command.CommandText = @"UPDATE [jiyuanNRLog] SET jyrzApprovalStatus = @jyrzApprovalStatus, 
                                                        jyrzApprovalDate = @jyrzApprovalDate,
                                                        jyrzApprovalBy = @jyrzApprovalBy WHERE jyrzLogId = @jyrzLogId ";

                        helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "DISAPPROVED");
                        helper.AddParameter("@jyrzApprovalDate", SqlDbType.DateTime, System.DateTime.Now.ToString());
                        helper.AddParameter("@jyrzApprovalBy", SqlDbType.Int, 1);    // TODO: 取用户id
                        helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, new Guid(jyrzLogId));    // TODO: 取用户id
                        retVal = helper.ExecuteNoneQuery();
                    }

                    transaction.Commit();
                    return retVal;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public override SqlDataReader executeDataReader()
        {
            string sqlText = null;
            List<SqlParameter> sqlParams = new List<SqlParameter>();

            if (string.IsNullOrEmpty(jyProjectId))
            {
                sqlText = @"SELECT jyId, jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, jyParentId,
                                jyProjectId, jyCreateOwner, jyIsMust, jyNewFlag, 
                                CONVERT(VARCHAR(100), jyCreateDate, 23) AS jyCreateDate,
                                (SELECT count(*) from [jiyuanNRLog] WHERE jyrzChangedId = aTbl.jyId 
                                    AND jyrzChangedType = 'UPDATE' AND jyrzApprovalStatus = 'UNAPPROVED') as jyUpdCount,
                                (SELECT count(*) from [jiyuanNRLog] WHERE jyrzChangedId = aTbl.jyId 
                                    AND jyrzChangedType = 'DELETE' AND jyrzApprovalStatus = 'UNAPPROVED') as jyDelCount 
                            FROM [jiyuanNR] AS aTbl WHERE (jyDeleteFlag <> 1)";
            }
            else
            {
                sqlText = @"SELECT jyId, jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, jyParentId, 
                                jyProjectId, jyCreateOwner, jyIsMust, jyNewFlag,
                                CONVERT(VARCHAR(100), jyCreateDate, 23) AS jyCreateDate, 
                                (SELECT count(*) from [jiyuanNRLog] WHERE jyrzChangedId = aTbl.jyId 
                                    AND jyrzChangedType = 'UPDATE' AND jyrzApprovalStatus = 'UNAPPROVED') as jyUpdCount, 
                                (SELECT count(*) from [jiyuanNRLog] WHERE jyrzChangedId = aTbl.jyId 
                                    AND jyrzChangedType = 'DELETE' AND jyrzApprovalStatus = 'UNAPPROVED') as jyDelCount 
                            FROM [jiyuanNR] AS aTbl WHERE (jyProjectId = @jyProjectId AND jyDeleteFlag <> 1)";
                sqlParams.Add(new SqlParameter("@jyProjectId", jyProjectId));
            }
            return SqlServerHelper.ExecuteReader(sqlText, sqlParams.ToArray());
        }
    }
}
